//
//  ModInfoViewController.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2018/1/5.
//  Copyright © 2018年 卢仕彤. All rights reserved.
//
#import "ModInfoViewController.h"
#import "FFGlobalHeader.h"
#import "ModInfoView.h"

#define kBaseSetInfoCell @"BaseSetInfoCell"

@interface ModInfoViewController () {
    NSMutableArray *_sections;
    BaseSetInfoCell *setInfoOneCell;
    NSString *_oneCellString;
    NSString *_typeString;
    //两个celltextFiled的文字
    NSString *_oneCellPlaceholderString;
}

@end

@implementation ModInfoViewController


#pragma mark - 保存的方法

- (void)saveAction {
    if (_type == modUserName) {
        if (setInfoOneCell.oneTextFiled.text.length > 0) {
            if (_delegate && [_delegate respondsToSelector:@selector(show_ModInfoViewC:dic:)]) {
                [_delegate show_ModInfoViewC:self dic:@{@"nickName":setInfoOneCell.oneTextFiled.text}.mutableCopy];
            }
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (_type == modCarreer) {
        if (setInfoOneCell.oneTextFiled.text.length > 0) {
            if (_delegate && [_delegate respondsToSelector:@selector(show_ModInfoViewC:dic:)]) {
                [_delegate show_ModInfoViewC:self dic:@{@"carrerrName":setInfoOneCell.oneTextFiled.text}.mutableCopy];
            }
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (_type == modCompanyName) {
        
        if (setInfoOneCell.oneTextFiled.text.length > 0) {
            if (_delegate && [_delegate respondsToSelector:@selector(show_ModInfoViewC:dic:)]) {
                [_delegate show_ModInfoViewC:self dic:@{@"companyName":setInfoOneCell.oneTextFiled.text}.mutableCopy];
            }
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark -  life

- (void)viewDidLoad {
    [super viewDidLoad];
    [self dealString];
    self.tableView.canReload = YES;
    [self registClass];
    [self addRightNavigationButtonWithTitle:@"保存"];
    [self.rightNavigationButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
}

#pragma mark - viewWillLayoutSubviews

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0 ,Main_Screen_Width, Main_Screen_Height);
}

- (void)registClass {
    [self.tableView registerClass:[BaseSetInfoCell class] forCellReuseIdentifier:kBaseSetInfoCell];
}

#pragma mark -  显示默认的cell的textFIled的值

- (void)dealString {
    if (self.type == modUserName) {
        _oneCellPlaceholderString = @"请填写你的姓名";
        self.title = @"修改姓名";
        
    }
    if (self.type == modCarreer) {
        _oneCellPlaceholderString = @"请填写你的职业名称";
        self.title = @"修改职业";
    }
    if (self.type == modCompanyName) {
        _oneCellPlaceholderString = @"请填写你的公司名称";
        self.title = @"修改公司";
    }
    
}

#pragma mark -  tableView

- (NSArray *)customSections {
    if (_sections == nil) {
        _sections = [NSMutableArray array];
        [_sections addObject:self.splitSection];
        MCUTableCustomSection *sec = [MCUTableCustomSection createWithTarget:self];
        sec.numberOfRowsSelector = @selector(numberOfRowForOneCell:);
        sec.cellForRowAtIndexSelector = @selector(cellViewForOneCell:);
        sec.heightForCellSelector = @selector(heightCellForOneCell:);
        [_sections addObject:sec];
    }
    
    return _sections;
}


#pragma mark -  oneCell

- (NSNumber *)numberOfRowForOneCell:(NSIndexPath *)indexPath {
    return @1;
}

- (UITableViewCell *)cellViewForOneCell:(NSIndexPath *)indexPath {
    setInfoOneCell = [[BaseSetInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kBaseSetInfoCell];
    [setInfoOneCell.oneTextFiled becomeFirstResponder];
    setInfoOneCell .oneTextFiled.placeholder = _oneCellPlaceholderString;
    if ([self.cellString length] > 0) {
        setInfoOneCell.oneTextFiled.text = self.cellString;
    }
    return setInfoOneCell;
}

- (NSNumber *)heightCellForOneCell:(NSIndexPath *)indexPath {
    return @55;
}

- (void)addGesture {
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissMissKeyBoard)];
    tableViewGesture.numberOfTapsRequired = 1;
    tableViewGesture.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:tableViewGesture];
}

- (void)dissMissKeyBoard {
    [setInfoOneCell.oneTextFiled endEditing:YES];
}

#pragma - dlegate(setter)

- (void)setDelegate:(id<ModInfoViewCDelegate>)delegate {
    _delegate = delegate;
}

@end
